package com.coollang.smartbra.presenter;

import android.graphics.Bitmap;
import android.os.Handler;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.PolylineOptions;
import com.coollang.smartbra.R;
import com.coollang.smartbra.activity.ShareMapActivity;
import com.coollang.smartbra.dialog.ShareBitmapDialog;
import com.coollang.smartbra.model.RunningDataModel;
import com.coollang.smartbra.utils.ShareUtils;
import com.coollang.smartbra.utils.WalkUtil;

import java.util.ArrayList;
import java.util.List;

import io.realm.Realm;
import io.realm.RealmResults;

/**
 * @Auther: Timmy
 * @Data: 2017/8/15
 * @Fuction:
 */

public class ShareMapActivityPresenter1 {

    private ShareMapActivity mShareMapActivity;
    private AMap mAMap;

    private ArrayList<LatLng> mLocationList = new ArrayList<>();
    private ShareUtils mShareUtils;
    private ShareBitmapDialog mShareBitmapDialog;

    public ShareMapActivityPresenter1(ShareMapActivity activityPresenter) {
        mShareMapActivity = activityPresenter;
    }

    public void initMap(MapView map) {
        if (mAMap == null) {
            mAMap = map.getMap();
            mAMap.getUiSettings().setRotateGesturesEnabled(false);
            mAMap.getUiSettings().setZoomControlsEnabled(true);
            mAMap.moveCamera(CameraUpdateFactory.zoomTo(4));
            mAMap.setMapTextZIndex(2);
        }
    }

    public void addPolyline() {

        setStartMarker(mLocationList.get(0));
        setEndMarker(mLocationList.get(mLocationList.size() - 1));

        PolylineOptions polylineOptions = new PolylineOptions();
        polylineOptions.addAll(mLocationList);
        polylineOptions.visible(true).width(15).zIndex(200);
        polylineOptions.colorValues(WalkUtil.getColorList(mLocationList.size() / 144 + 1, mShareMapActivity));

        polylineOptions.useGradient(true);
        mAMap.addPolyline(polylineOptions);

        // -------------------------
        LatLngBounds.Builder newbounds = new LatLngBounds.Builder();

        for (LatLng latLng : mLocationList) {
            newbounds.include(latLng);
        }

        mAMap.animateCamera(CameraUpdateFactory.newLatLngBounds(newbounds.build(),
                15));//第二个参数为四周留空宽度

    }

    public void setLocationList(ArrayList<LatLng> locationList) {
        mLocationList = locationList;
    }

    public RunningDataModel queryRunningData() {

        Realm mRealm = Realm.getDefaultInstance();
        RealmResults<RunningDataModel> results = mRealm.where(RunningDataModel.class).findAll();
        List<RunningDataModel> actionList = mRealm.copyFromRealm(results);

        return actionList.get(0);
    }

    // 开始的按钮
    private void setStartMarker(LatLng latlng) {

        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.setFlat(false);
        markerOptions.anchor(0.5f, 0.7f);

        markerOptions.zIndex(1);
        ImageView iv = new ImageView(mShareMapActivity);
        FrameLayout.LayoutParams fmIv = new FrameLayout.LayoutParams(35, 35);
        iv.setImageResource(R.drawable.start);
        iv.setLayoutParams(fmIv);
        BitmapDescriptor markerIcon = BitmapDescriptorFactory.fromView(iv);
        markerOptions.icon(markerIcon);
        markerOptions.position(latlng);
        mAMap.addMarker(markerOptions);
    }

    private void setEndMarker(LatLng latlng) {
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.setFlat(false);
        markerOptions.anchor(0.5f, 0.7f);

        markerOptions.zIndex(1);
        ImageView iv = new ImageView(mShareMapActivity);
        FrameLayout.LayoutParams fmIv = new FrameLayout.LayoutParams(35, 35);
        iv.setImageResource(R.drawable.end);
        iv.setLayoutParams(fmIv);
        BitmapDescriptor markerIcon = BitmapDescriptorFactory.fromView(iv);
        markerOptions.icon(markerIcon);
        markerOptions.position(latlng);
        mAMap.addMarker(markerOptions);
    }

    public void screenShot() {
        // 高德地图截屏
        mAMap.getMapScreenShot(mOnMapScreenShotListener);
    }

    private Handler mHandler = new Handler();

    private AMap.OnMapScreenShotListener mOnMapScreenShotListener = new AMap.OnMapScreenShotListener() {
        @Override
        public void onMapScreenShot(Bitmap bitmap) {
            mShareMapActivity.mFrameLayoutmap.setImageBitmap(bitmap);
            mShareMapActivity.mFrameLayoutmap.setVisibility(View.VISIBLE);

            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mShareUtils.initShare();
                    mShareBitmapDialog.show();
                }
            }, 1500);
        }

        @Override
        public void onMapScreenShot(Bitmap bitmap, int status) {
        }
    };

    public void initShare() {
        if (mShareUtils == null) {
            mShareUtils = new ShareUtils(mShareMapActivity);
            mShareBitmapDialog = new ShareBitmapDialog(mShareMapActivity, null);
            mShareBitmapDialog.setOnclickListener(mOnclickListener);
            mShareUtils.setResult(mShareResult);
        }
    }

    private ShareBitmapDialog.OnclickListener mOnclickListener = new ShareBitmapDialog.OnclickListener() {
        @Override
        public void onCancel() {
            mShareBitmapDialog.dismiss();
        }

        @Override
        public void onWeixin() {
            mShareUtils.share(1);
        }

        @Override
        public void onWEXIN_CIRCLE() {
            mShareUtils.share(2);
        }

        @Override
        public void onWeibo() {
            mShareUtils.share(3);
        }

        @Override
        public void onQQ() {
            mShareUtils.share(4);
        }
    };

    private ShareUtils.ShareResult mShareResult = new ShareUtils.ShareResult() {
        @Override
        public void onShareSuccess() {
            mShareMapActivity.mFrameLayoutmap.setVisibility(View.GONE);
        }

        @Override
        public void onShareFaild() {
            mShareMapActivity.mFrameLayoutmap.setVisibility(View.GONE);
        }

        @Override
        public void onShareCancel() {
            mShareMapActivity.mFrameLayoutmap.setVisibility(View.GONE);
        }
    };
}
